package com.tenbit.pdt.svc;

import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.tenbit.com.vo.ErrorMsgVO;
import com.tenbit.emp.dao.CodeZipEDAO;
import com.tenbit.emp.dao.EmployeeQEMP1000DAO;
import com.tenbit.emp.vo.EMP1000VO;
import com.tenbit.pdt.dao.ItemInfoEDAO;
import com.tenbit.pdt.dao.ProductInfoEDAO;
import com.tenbit.pdt.dao.ProductInfoQ1000DAO;
import com.tenbit.pdt.vo.ItemInfoVO;
import com.tenbit.pdt.vo.ProductInfoVO;

import emp.tenbit.emp.bean.PagingBean;

@Service
public class PDT1000SVCImpl implements PDT1000SVC{
	@Autowired
	private ProductInfoQ1000DAO qdao;
	@Autowired
	private EmployeeQEMP1000DAO qemp1000dao;
	@Autowired
	private CodeZipEDAO zipEDAO;
	@Autowired
	private ItemInfoEDAO itemInfoEDAO;
	@Autowired
	private ProductInfoEDAO productInfoEDAO;
	@Autowired
	private ProductInfoQ1000DAO productInfoQ1000DAO;
	
	private Logger log=Logger.getLogger(this.getClass());
	
	
	
	@Override
	public ErrorMsgVO doValidateForm(ProductInfoVO vo,MultipartFile file) {
		String employeeName=vo.getEmployeeName();
		String productCode=vo.getProductCode();
		String productName=vo.getProductName();
		String itemCode=vo.getItemCode();
		String manufactureCode=vo.getManufactureCode();
		String standard=vo.getStandard();
		String unitCode=vo.getUnitCode();
		BigDecimal unitPrice=(BigDecimal)vo.getUnitPrice();
		String fileName=(String)file.getOriginalFilename();
		ErrorMsgVO errorMsgVO = new ErrorMsgVO();
		
		if(employeeName.equals("")){
			errorMsgVO.setErrMsg("사원번호를 입력하세요");
			errorMsgVO.setErrExist(true);
			
			return errorMsgVO;
		}
		if(productCode.equals("")){
			errorMsgVO.setErrMsg("상품코드를 입력하세요");
			errorMsgVO.setErrExist(true);
			
			return errorMsgVO;
		}
		if(productName.equals("")){
			errorMsgVO.setErrMsg("상품명을 입력하세요");
			errorMsgVO.setErrExist(true);
			
			return errorMsgVO;
		}
		if(itemCode.equals("")){
			errorMsgVO.setErrMsg("품목명을 입력하세요");
			errorMsgVO.setErrExist(true);
			
			return errorMsgVO;
		}
		if(manufactureCode.equals("")){
			errorMsgVO.setErrMsg("제조사를 입력하세요");
			errorMsgVO.setErrExist(true);
			
			return errorMsgVO;
		}
		if(standard.equals("")){
			errorMsgVO.setErrMsg("규격을 입력하세요");
			errorMsgVO.setErrExist(true);
			
			return errorMsgVO;
		}
		if(unitCode.equals("")){
			errorMsgVO.setErrMsg("단위를 입력하세요");
			errorMsgVO.setErrExist(true);
			
			return errorMsgVO;
		}
		if(unitPrice == null){
			errorMsgVO.setErrMsg("단가를 입력하세요");
			errorMsgVO.setErrExist(true);
			
			return errorMsgVO;
		}
		if(fileName.equals("")){
			errorMsgVO.setErrMsg("파일 이름을 입력하세요");
			errorMsgVO.setErrExist(true);
			
			return errorMsgVO;
		}
		
		return errorMsgVO;
	}

	@Override
	public void delProductInfo(ProductInfoVO vo) {
		try {
			productInfoEDAO.deleteProductInfo(vo);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setProductInfo(ProductInfoVO productInfoVO) {
		
		try {
			productInfoEDAO.updateProductInfo(productInfoVO);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public Map<String, Object> getProductInfo(String productNum) {
		Map<String, Object> map=null;
		
		try {
			map=productInfoQ1000DAO.getProductInfo(productNum);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return map;
	}

	@Override
	public void insPDTInfo(ProductInfoVO vo) {
		
		
		vo.setProductNum(productInfoEDAO.selectProductInfoSeq());
		
		log.debug("insPDTInfo "+vo);
		
		try {
			productInfoEDAO.insertProductInfo(vo);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void doUploadFile(MultipartFile file) {
		String path="C:\\TB\\WORKSHOP\\workspace\\tbworkshop\\WebContent\\upload\\";
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(path+file.getOriginalFilename());
			byte []fileData=file.getBytes();
			
			fos.write(fileData);
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@Override
	public String getCategoryName(String code) {
		String ctgName=null;
		
		try {
			ctgName= qemp1000dao.selectCtgName(code);
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ctgName;
	}
	
	@Override
	public Map<String, List<EMP1000VO>> getCodeZipList() {
		List<EMP1000VO> list=null;
		
		try {
			list = zipEDAO.listCodeZipByMv();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Map<String, List<EMP1000VO>> map = new HashMap<String, List<EMP1000VO>>();
		List<EMP1000VO> clist = new ArrayList<EMP1000VO>();
		List<EMP1000VO> mlist = new ArrayList<EMP1000VO>();
		List<EMP1000VO> ulist = new ArrayList<EMP1000VO>();
		
		for (EMP1000VO vo : list) {
			
			if( vo.getCode_type().equals("CATEGORY")){
				clist.add(vo);
			}else if( vo.getCode_type().equals("MANUFACTURE") ){
				mlist.add(vo);
			}else if( vo.getCode_type().equals("UNIT") ){
				ulist.add(vo);
			}
		}
		
		map.put("CATEGORY", clist);
		map.put("MANUFACTURE", mlist);
		map.put("UNIT", ulist);
		
		return map;
	}

	@Override
	public List<ItemInfoVO> getItemNameList() {
		List<ItemInfoVO> list=null;
		
		try {
			list=itemInfoEDAO.listItemInfo(new HashMap<String, Object>());
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return 	list;
	}

	

	@Override
	public ErrorMsgVO doValidateFormDate(Map<String, Object> paraMap) {
		ErrorMsgVO errorMsgVO = new ErrorMsgVO();
		
		if( paraMap.size() == 0){
			return errorMsgVO;
		}
		
		String searchSel1=(String)paraMap.get("saleSearch1");
		String searchSel2=(String)paraMap.get("saleSearch2");
		
		if( searchSel1.equals("") || searchSel2.equals("") ){
			return errorMsgVO;
		}
		int year1 = Integer.parseInt(searchSel1.substring(0, 5));
		int month1 = Integer.parseInt(searchSel1.substring(5, 7));
		int day1 = Integer.parseInt(searchSel1.substring(7));
		
		int year2 = Integer.parseInt(searchSel2.substring(0, 5));
		int month2 = Integer.parseInt(searchSel2.substring(5, 7));
		int day2 = Integer.parseInt(searchSel2.substring(7));
				
		if(year1 > year2 ){
			errorMsgVO.setErrMsg("년이 더 큽니다.");
			errorMsgVO.setErrExist(true);
		}else if( year1 == year2 ){
			if( month1 > month2 ){
				errorMsgVO.setErrMsg("월이 더 큽니다.");
				errorMsgVO.setErrExist(true);
			}else if( month1 == month2 ){
				if( day1 > day2 ){
					errorMsgVO.setErrMsg("일이 더 큽니다.");
					errorMsgVO.setErrExist(true);
				}
			}
		}
		
		return errorMsgVO;
	}
	
	@Override
	public List<Map<String, Object>> getProductInfoList(String page,Map<String, Object> paraMap) {
		List<Map<String, Object>> list=null;
		
		try {
		   int pageScale=10;
		   int currentPage=0;
		   
		   try{
		   String tempPage="";
		   tempPage=page;
		   if(tempPage==null)tempPage="1";
			   currentPage=Integer.parseInt(tempPage);
			   int start=1+(currentPage-1)*pageScale;
			   int end=pageScale+(currentPage-1)*pageScale;
			   paraMap.put("start", start);
			   paraMap.put("end", end);
			   
		   }catch(Exception e){
			   currentPage=1;
		   }
		   list = qdao.listProductInfo(paraMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return list;
	}

	public PagingBean getPageBean(String page,Map<String, Object> paraMap){
		   int pageScale=10;
		   int currentPage=0;
		   int blockScale=10;
		   int startPage=0;
		   int endPage=0;
		   int currentBlock=0;
		   int totalPage=0;
		   
		   try{
		   int totalRow=productInfoQ1000DAO.selectCountProductInfo(paraMap);
		   totalPage=totalRow%pageScale==0?totalRow/pageScale:(totalRow/pageScale)+1;
		   if(totalRow==0)totalPage=1;
		   String tempPage="";
		   tempPage=page;
		   if(tempPage==null)tempPage="1";
			   currentPage=Integer.parseInt(tempPage);
			   currentBlock=currentPage%blockScale==0?currentPage/blockScale:currentPage/blockScale+1;
			   startPage=1+(currentBlock-1)*blockScale;
			   endPage=blockScale+(currentBlock-1)*blockScale;
			   if(totalPage<=endPage){
				   endPage=totalPage;
			   }
			   
		   }catch(Exception e){
			   currentPage=1;
		   }
		   
		   return new PagingBean(startPage, endPage, currentPage, currentBlock, totalPage);
	}
}
